package db

import (
	"errors"
	"hr/api/dto"

	"gorm.io/gorm"
)

type RecruitmentManage struct {
	AppModel
	Name         string `gorm:"size:64" json:"name"`
	Money        string `gorm:"size:64" json:"money"`
	Latitude     string `gorm:"size:64" json:"latitude"`
	Longitude    string `gorm:"size:64" json:"longitude"`
	Tag          string `gorm:"size:255" json:"tag"`
	Address      string `gorm:"size:255" json:"address"`
	Describe     string `json:"describe"`
	DescribeText string `json:"describe_text"`
	CreateBy     string `gorm:"size:128" json:"createBy"` // 创建人
	UpdateBy     string `gorm:"size:128" json:"updateBy"` // 更新人
	Remark       string `gorm:"size:255" json:"remark"`   //备注
	Status       string `gorm:"size:64;" json:"status"`
	Dept         string `gorm:"size:64;" json:"dept"`
}

func (m *RecruitmentManage) TableName() string {
	return "recruitment_manage"
}

type RecruitmentPageModel struct {
	Total    int64               `json:"total"`
	Result   []RecruitmentManage `json:"result"`
	PageNo   int                 `json:"pageNo"`
	PageSize int                 `json:"pageSize"`
}

func GetRecruitmentPaging(pagination dto.Pagination, name string) (RecruitmentPageModel, error) {
	result := RecruitmentPageModel{
		Total:    0,
		Result:   make([]RecruitmentManage, 0),
		PageNo:   pagination.PageNo,
		PageSize: pagination.PageSize,
	}
	offset := (pagination.PageNo - 1) * pagination.PageSize
	dbs := GetInstance().Model(&RecruitmentManage{})
	if name != "" {
		dbs = dbs.Where("name LIKE ?", "%"+name+"%")
	}

	dbs = dbs.Order("created_at desc")

	if err := dbs.Offset(offset).Limit(pagination.PageSize).Find(&result.Result).Error; err != nil {
		return result, err
	}
	if err := dbs.Offset(-1).Limit(-1).Count(&result.Total).Error; err != nil {
		return result, err
	}
	return result, nil
}

// GetRecruitment 根据条件查找
func GetRecruitment(p map[string]interface{}) (RecruitmentManage, error, string) {
	f := RecruitmentManage{}
	err := GetInstance().Where(p).First(&f).Error
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return f, errors.New("查无记录"), "0"
		}
		return f, errors.New(err.Error()), "1"
	}
	return f, nil, "2"
}

func CreateRecruitment(p dto.RecruitmentRequest, user string) (RecruitmentManage, error) {
	d := RecruitmentManage{
		Name:         p.Name,
		Money:        p.Money,
		Tag:          p.Tag,
		Address:      p.Address,
		Describe:     p.Describe,
		DescribeText: p.DescribeText,
		CreateBy:     user,
		Longitude:    p.Longitude,
		Latitude:     p.Latitude,
		Remark:       p.Remark,
		Status:       p.Status,
		Dept:         p.Dept,
	}
	if err := GetInstance().Create(&d).Error; err != nil {
		return d, err
	}
	return d, nil
}

func UpdateRecruitment(id uint, p dto.RecruitmentRequest, userId string) (RecruitmentManage, error) {
	f := RecruitmentManage{
		AppModel: AppModel{
			ID: id,
		},
	}
	err := GetInstance().Model(&f).Updates(RecruitmentManage{
		Name:         p.Name,
		Money:        p.Money,
		Tag:          p.Tag,
		Address:      p.Address,
		Describe:     p.Describe,
		DescribeText: p.DescribeText,
		UpdateBy:     userId,
		Remark:       p.Remark,
		Longitude:    p.Longitude,
		Latitude:     p.Latitude,
		Status:       p.Status,
		Dept:         p.Dept,
	}).Updates(map[string]interface{}{
		"id": id,
	}).Scan(&f).Error
	if err != nil {
		return f, err
	}
	return f, nil
}

func DeleteRecruitment(id uint) (RecruitmentManage, error) {
	f := RecruitmentManage{}
	err := GetInstance().Unscoped().Where("id = ?", id).Delete(&f).Error
	if err != nil {
		return f, err
	}
	return f, nil
}
